package com.zx.Ding.controller;/**
 * Author:JasonZ
 * Data:2023/3/17
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.Ding.common.R;
import com.zx.Ding.entity.CategoryComment;
import com.zx.Ding.entity.CategoryDetail;
import com.zx.Ding.service.CategoryCommentService;
import com.zx.Ding.service.CategoryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * @program: Ding
 *
 * @description:
 *
 * @author: JasonZ
 *
 * @create: 2023-03-17 11:58
 **/
@RestController
@RequestMapping("/categoryComment")
public class CategoryCommentController {
    @Autowired
    private CategoryCommentService categoryCommentService;

    @Autowired
    private CategoryDetailService categoryDetailService;

    @GetMapping
    public R<List<CategoryComment>>listByParams(CategoryComment categoryComment){
        System.out.println(categoryComment);
        LambdaQueryWrapper<CategoryComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryComment.getCategoryId()!=null,CategoryComment::getCategoryId,categoryComment.getCategoryId());
        queryWrapper.eq(CategoryComment::getUserId,categoryComment.getUserId());
        List<CategoryComment> list = categoryCommentService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping
    public R<String>save(@RequestBody CategoryComment categoryComment){
        System.out.println(categoryComment);
        categoryCommentService.sava(categoryComment);
        return R.success("success");
    }

    @GetMapping("/{categoryId}")
    public R<List<CategoryComment>> listByCategoryId(@PathVariable Long categoryId){
        LambdaQueryWrapper<CategoryComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,CategoryComment::getCategoryId,categoryId);
        List<CategoryComment> list = categoryCommentService.list(queryWrapper);
        return R.success(list);
    }

    @DeleteMapping
    public R<String>delById(Long id){
        CategoryComment comment = categoryCommentService.getById(id);
        //先获得对应的店铺ID
        Long categoryId = comment.getCategoryId();
        //在进行删除
        categoryCommentService.removeById(id);
        LambdaQueryWrapper<CategoryDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryDetail::getCategoryId,categoryId);
        CategoryDetail categoryDetail = categoryDetailService.getOne(queryWrapper);
        //总条数减一
        categoryDetail.setCommentTotal(categoryDetail.getCommentTotal()-1);
        //门店平均分变化
        LambdaQueryWrapper<CategoryComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryComment::getCategoryId,categoryId);
        List<CategoryComment> comments = categoryCommentService.list(wrapper);
        //求该门店的总分和。
        QueryWrapper<CategoryComment> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select(("IFNULL(sum(rating),0) as totalScore"))
                .eq("category_id", categoryId);
        Map<String, Object> map = categoryCommentService.getMap(queryWrapper1);
        BigDecimal sumCount = (BigDecimal) map.get("totalScore");
        //得到平均分
        System.out.println("------"+comments.size()+"-----"+sumCount);
        BigDecimal result = sumCount.divide(BigDecimal.valueOf(comments.size()), 2, RoundingMode.HALF_UP);
        categoryDetail.setScore(result);
        categoryDetailService.updateById(categoryDetail);
        return R.success("删除成功");
    }
}